//
//  WYReceiveDetailController.m
//  Boss
//
//  Created by admin on 2018/6/8.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYReceiveDetailController.h"
#import "WYRedPacketListCell.h"
#import "WYRedPacketMessageHeadCell.h"
#import "WYReceivePeopleMessage.h"
#import "WYPersonWebHomePageController.h"
#import "WYVIPController.h"
@interface WYReceiveDetailController ()<UITableViewDelegate,UITableViewDataSource,WYRedPacketMessageHeadCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)BOOL refresh;
@property (nonatomic,assign)NSUInteger page;
@end

@implementation WYReceiveDetailController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.page = 1;
    [self setupUI];
    [self loadData];
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        weakSelf.page++;
         [weakSelf loadData];
       // [weakSelf loadDataWithYear:self.year];
    }];
    
    
}

-(void)loadData
{
//    [self.dataArray removeAllObjects];
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getRedPacketReceiveListWithRedpacketId:self.redPacket.redpacketId WithCurrentPage:page WithRedpacketType:self.redPacket.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            if (weakSelf.refresh) {
                [weakSelf.dataArray removeAllObjects];
            }
//            [weakSelf.dataArray addObject:[WYReceivePeopleMessage getReceivePeopleArrayFromJson:response[@"result"][@"detail"]]];
            [weakSelf.dataArray addObjectsFromArray:[WYReceivePeopleMessage getReceivePeopleArrayFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            
            
            
        }else{
            
            
        }
        
        
        
        
        
    }];
    
}


-(void)setupUI
{
    self.navigationItem.title = @"领取详情";
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRedPacketMessageHeadCell" bundle:nil] forCellReuseIdentifier:@"WYRedPacketMessageHeadCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRedPacketListCell" bundle:nil] forCellReuseIdentifier:@"WYRedPacketListCell"];
    
     self.tableView.estimatedRowHeight = 44;
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
     [self wr_setNavBarShadowImageHidden:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0&&indexPath.row==0) {
        WYRedPacketMessageHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYRedPacketMessageHeadCell" forIndexPath:indexPath];
        
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"WYRedPacketMessageHeadCell" owner:nil options:nil].firstObject;
        }
        [cell setType:@"521"];
        
        
        if (self.redMessage) {
            [cell setRedMessage:self.redMessage];
        }
        if (self.advertisementMessage) {
            [cell setAdvertisementMessage:self.advertisementMessage];
        }
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    WYRedPacketListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYRedPacketListCell" forIndexPath:indexPath];
    if (!cell) {
        cell =[[NSBundle mainBundle]loadNibNamed:@"WYRedPacketListCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setMessage:self.dataArray[indexPath.row]];
    return cell;
    
    
}

-(void)gotoVip
{
    WYVIPController *vpc = [[WYVIPController alloc]init];
    [self.navigationController pushViewController:vpc animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return;
    }
    WYReceivePeopleMessage *userMessage =self.dataArray[indexPath.row];
    WYPersonWebHomePageController *web = [[WYPersonWebHomePageController alloc]init];
    web.urlString = userMessage.userId;
    [self.navigationController pushViewController:web animated:YES];
    
    
}
-(void)gotoPageWithUserId:(NSString *)userId
{
    WYPersonWebHomePageController *web = [[WYPersonWebHomePageController alloc]init];
    
    web.urlString = userId;
    [self.navigationController pushViewController:web animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    if (offset.y <= 0) {
        offset.y = 0;
    }
    scrollView.contentOffset = offset;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
    }
    return _tableView;
}
@end
